//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>
#include <pci.h>

/*
 * General constants that are fun to know.
 *
 * RealTek PCI vendor ID
 */
#define    RT_VENDORID                0x10EC

/*
 * RealTek chip device IDs.
 */
#define    RT_DEVICEID_8129            0x8129
#define    RT_DEVICEID_8139            0x8139
#define    RT_DEVICEID_8139CPlus       (0x8139+1)    /*  For 8139C+ use */

/*
 * Accton PCI vendor ID
 */
#define ACCTON_VENDORID                0x1113

/*
 * Accton MPX 5030/5038 device ID.
 */
#define ACCTON_DEVICEID_5030           0x1211

/*
 * Delta Electronics Vendor ID.
 */
#define DELTA_VENDORID                 0x1500

/*
 * Delta device IDs.
 */
#define DELTA_DEVICEID_8139            0x1360
/*
 * Addtron vendor ID.
 */
#define ADDTRON_VENDORID               0x4033

/*
 * Addtron device IDs.
 */
#define ADDTRON_DEVICEID_8139          0x1360

/*
 * D-Link vendor ID.
 */
#define DLINK_VENDORID                 0x1186

/*
 * D-Link DFE-530TX+ device ID
 */
#define DLINK_DEVICEID_530TXPLUS       0x1300

/*
 * Various supported device vendors/types and their names.
 */
struct rl_type {
    UInt16        rl_vid;
    UInt16        rl_did;
    char          *rl_name;
};

static struct rl_type rl_devs[] = {
    { RT_VENDORID, RT_DEVICEID_8129,
                                           "RealTek 8129 10/100BaseTX" },
    { RT_VENDORID, RT_DEVICEID_8139,
                                           "RealTek 8139 10/100BaseTX" },
    { RT_VENDORID, RT_DEVICEID_8139CPlus,
                                         "RealTek 8139C+ 10/100BaseTX" },
    { ACCTON_VENDORID, ACCTON_DEVICEID_5030,
                                    "Accton MPX 5030/5038 10/100BaseTX" },
    { DELTA_VENDORID, DELTA_DEVICEID_8139,
                                  "Delta Electronics 8139 10/100BaseTX" },
    { ADDTRON_VENDORID, ADDTRON_DEVICEID_8139,
                                  "Addtron Technolgy 8139 10/100BaseTX" },
    { DLINK_VENDORID, DLINK_DEVICEID_530TXPLUS,
                                       "D-Link DFE-530TX+ 10/100BaseTX" },
    { 0, 0, NULL }
};

#define INFO_CONN_TYPE 0
#define INFO_SLOT_NO 1
#define MY_INFOS 2


/*
 * Probe for a RealTek 8129/8139 chip. Check the PCI vendor and device
 * IDs against our list and return a device name if we find a match.
 */
static pciConfigPtr
Rtl8139_Probe(pciConfigPtr *pcrpp)
{
    struct rl_type *t;
    int i = 0;
    static int found = 0;
    Boolean flag = FALSE;
    pciConfigPtr pcrp = NULL;

    while ((pcrp = pcrpp[i])) {
        for (t = rl_devs; t->rl_vid != 0; t++) {
            if ((pcrp->_vendor == t->rl_vid) && (pcrp->_device == t->rl_did)) {
                if (found == 1)
                    found = 0;
                else {
                    found = 1;
                    flag = TRUE;
                    break;
                }
            }
        }
        if ((found == 1) && (flag == TRUE))
           break;
        i++;
    }

    if (found == 1)
        return pcrp;
    else
        return 0;
}


void getdriverinfo(int * pirq, int * piobase)
{
    UInt32 Id;
    pciTagRec tag;
    pciConfigPtr *pcrpp, pcrp;

    pcrpp = scanpci(0);
    
    if ((pcrp = Rtl8139_Probe(pcrpp)) == NULL) {
       kprintf("can't find rtl8139 network card!!!!\n");
       return;
    }

    tag = pcibusTag(pcrp->_bus, pcrp->_cardnum, pcrp->_func);
    Id = pciReadLong(tag, 0x3c);
    *pirq = Id & 0xff;

    Id = pciReadLong(tag, 0x10);
    *piobase = (IO_PORT)(Id & ~0xf);

    return;
}
